﻿/*
 * Programer: JR Padfield
 * Description: Login Screen handles logging into the server.
 * Version: 1
 * Date: 12/17/13 * 
 */

using System;
using TheWorldLibrary.Controls;
using Microsoft.Xna.Framework;
using TheWorld.Networking;
using TheWorldLibrary.GameStates;
using TheWorld.Global;
using Microsoft.Xna.Framework.Content;

namespace TheWorld.GameScreens
{
    public class LoginScreen : BaseGameState
    {
        #region Field region

        GfxManager GfxM = GfxManager.Instance;
        //General Grnl = General.Instance;

        TextBox userName, userPass;
        Button btnLogin, btnBack;
        Label lblName, lblPass;

        #endregion

        #region Constructor region

        public LoginScreen(Game game, GameStateManager manager)
            : base(game, manager)
        {
        }

        #endregion

        #region XNA Method region

        protected override void LoadContent()
        {
            ContentManager Content = GameRef.Content;

            base.LoadContent();

            CreateMenu();

        }

        public override void Update(GameTime gameTime)
        {
            ControlManager.Update(gameTime, PlayerIndex.One);

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GameRef.SpriteBatch.Begin();

            base.Draw(gameTime);
            ControlManager.Draw(GameRef.SpriteBatch);

            GameRef.SpriteBatch.End();
        }

        #endregion

        #region Title Screen Methods

        void CreateMenu()
        {
            userName = new TextBox(GameRef, GfxM.FONT_TEXTURE[1], GfxM.Gui[3], GfxM.Gui[13], GameRef.SpriteBatch);
            userName.Position = new Vector2(512 - 128, 325);
            Components.Add(userName);

            userPass = new TextBox(GameRef, GfxM.FONT_TEXTURE[1], GfxM.Gui[3], GfxM.Gui[13], GameRef.SpriteBatch);
            userPass.Position = new Vector2(512 - 128, 380);
            Components.Add(userPass);

            btnLogin = new Button(GfxM.Buttons[15], GfxM.Buttons_H[15], GfxM.Buttons_C[15], new Vector2(363, 450), true);
            btnLogin.Clicked += new EventHandler(btnLogin_Clicked);
            ControlManager.Add(btnLogin);

            btnBack = new Button(GfxM.Buttons[6], GfxM.Buttons_H[6], GfxM.Buttons_C[6], new Vector2(520, 450), true);
            btnBack.Clicked += new EventHandler(btnBack_Clicked);
            ControlManager.Add(btnBack);

            lblName = new Label();
            lblName.Text = "UserName:";
            lblName.Position = new Vector2(512 - 128, 300);
            ControlManager.Add(lblName);

            lblPass = new Label();
            lblPass.Text = "Password:";
            lblPass.Position = new Vector2(512 - 128, 355);
            ControlManager.Add(lblPass);
        }

        void btnLogin_Clicked(object sender, EventArgs e)
        {
            ClientTcp.Instance.SendLogin(userName.Text, General.Instance.CalculateMD5Hash(userPass.Text));
        }

        void btnBack_Clicked(object sender, EventArgs e)
        {
            Transition(ChangeType.Change, Client.MainMenuScreen);
        }

        #endregion
    }
}
